package edu.zut.arrays;

import java.util.*;
import java.util.function.BiConsumer;


public class MapDemo {

    public static void main(String[] args) {
        //map
        HashMap<String,Stu> map = new HashMap<>();
        //将key 保存为一个set集合   操作  都是根据key操作
        //添加元素   key 全局唯一    value  任意
        map.put("2020201444442",new Stu("张三",89));
        map.put("2020201444443",new Stu("李四",83));
        map.put("2020201444444",new Stu("王五",84));
        map.put("2020201444445",new Stu("赵六",83));
        //修改   修改value
        map.put("2020201444444",new Stu("tom",84));
        System.out.println(map);
        //删除
        map.remove("2020201444445");
        //获取元素
        Stu stu = map.get("2020201444444");
        System.out.println("stu = " + stu);

        //遍历map
        //获取当前map的key值集合
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(map.get(key));
        }
        System.out.println("---------------------------");
        //直接获取value集合
        Collection<Stu> values = map.values();
        for (Stu value : values) {
            System.out.println("value = " + value);
        }
        System.out.println("---------------------------");
        //利用entrySet获取一个Map.Entry类型的set集合
        Set<Map.Entry<String, Stu>> entries = map.entrySet();
        for (Map.Entry<String, Stu> entry : entries) {
            System.out.println("key:" + entry.getKey());
            System.out.println("value:" + entry.getValue());
        }
        System.out.println("---------------------------");
        map.forEach(new BiConsumer<String, Stu>() {
            //accept 方法中的第一个参数就是map的key
            //第二个参数就是map的value
            @Override
            public void accept(String key, Stu value) {
                System.out.println("key = " + key);
                System.out.println("value = " + value);
            }
        });
    }

    //如果保存商品信息
    static void testMap(){
        Map<String,Map<String, List<String>>> map = new HashMap<>();
        Map<String, List<String>> map2 = new HashMap<>();
        List<String> list = new ArrayList<>();
        list.add("小米");
        list.add("iphone");
        list.add("华为");
        map2.put("热门手机",list);
        List<String> list2 = new ArrayList<>();
        list2.add("合约");
        map2.put("特色手机",list2);
        map.put("手机",map2);
    }

}
